package com.ruoyi.system.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.system.api.domain.SysUserAccount;
import com.ruoyi.system.service.ISysUserAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 用户账号Controller
 *
 * @author felix
 * @date 2022-04-25
 */
@Api(tags = " 用户账号")
@RestController
@RequestMapping("/sysUserAccount")
public class SysUserAccountController extends BaseController {
    @Autowired
    private ISysUserAccountService sysUserAccountService;

    /**
     * 查询用户账号列表
     */
    @ApiOperation(value="查询用户账号列表", notes="查询用户账号列表")
    @PreAuthorize(hasPermi = "system:sysUserAccount:list")
    @GetMapping("/list")
    public TableDataInfo list(SysUserAccount sysUserAccount) {
        startPage();
        List<SysUserAccount> list = sysUserAccountService.selectSysUserAccountList(sysUserAccount);
        return getDataTable(list);
    }

    /**
     * 导出用户账号列表
     */
    @ApiOperation(value="导出用户账号列表", notes="导出用户账号列表")
    @PreAuthorize(hasPermi = "system:sysUserAccount:export")
    @Log(title = "用户账号", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysUserAccount sysUserAccount) throws IOException {
        List<SysUserAccount> list = sysUserAccountService.selectSysUserAccountList(sysUserAccount);
        ExcelUtil<SysUserAccount> util = new ExcelUtil<SysUserAccount>(SysUserAccount.class);
        util.exportExcel(response, list, "用户账号数据");
    }
    /**
     * 获取用户账号详细信息
     */
    @ApiOperation(value="获取用户账号详细信息", notes="获取用户账号详细信息")
    @PreAuthorize(hasPermi = "system:sysUserAccount:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(sysUserAccountService.selectSysUserAccountById(id));
    }

    /**
     * 新增用户账号
     */
    @ApiOperation(value="新增用户账号", notes="新增用户账号")
    @PreAuthorize(hasPermi = "system:sysUserAccount:add")
    @Log(title = "用户账号", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysUserAccount sysUserAccount) {
        return toAjax(sysUserAccountService.insertSysUserAccount(sysUserAccount));
    }

    @PostMapping(value = "/add")
    public R addUserAccountInfo(@RequestBody SysUserAccount userAccount){
        return R.ok(sysUserAccountService.insertSysUserAccount(userAccount));
    }

    @PostMapping(value = "/update")
    public R updateUserAccountInfo(@RequestBody SysUserAccount userAccount){
        return R.ok(sysUserAccountService.updateSysUserAccount(userAccount));
    }

    /**
     * 修改用户账号
     */
    @ApiOperation(value="修改用户账号", notes="修改用户账号")
    @PreAuthorize(hasPermi = "system:sysUserAccount:edit")
    @Log(title = "用户账号", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysUserAccount sysUserAccount) {
        return toAjax(sysUserAccountService.updateSysUserAccount(sysUserAccount));
    }

    /**
     * 删除用户账号
     */
    @ApiOperation(value="删除用户账号", notes="删除用户账号")
    @ApiImplicitParam(name = "ids", value = "用户账号ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "system:sysUserAccount:remove")
    @Log(title = "用户账号", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(sysUserAccountService.deleteSysUserAccountByIds(ids));
    }

    /**
     * 获取当前用户信息
     */
    @PostMapping("/info")
    public R<SysUserAccount> getUserAccountInfo(@RequestBody SysUserAccount userAccount) {
        List<SysUserAccount> sysUserAccount = sysUserAccountService.selectSysUserAccountList(userAccount);
        return CollectionUtils.isEmpty(sysUserAccount)?R.ok(null):R.ok(sysUserAccount.get(0));
    }

    @ApiOperation(value = "APP绑定cid", notes = "APP绑定cid")
    @GetMapping("/app/bind")
    public AjaxResult bindAppCid(@RequestParam("clientId")String clientId,@RequestParam("uId")String uId ){
        return AjaxResult.success(sysUserAccountService.updateUserAccountCid(clientId,uId));
    }

    /**
     * 获取当前用户信息
     */
//    @GetMapping("/info/{wxId}")
//    public R<SysUserAccount> info(@PathVariable("wxId") String wxId) {
//        SysUserAccount sysUserAccount = sysUserAccountService.selectSysUserAccountByWxId(wxId);
//        return R.ok(sysUserAccount);
//    }

    /**
     * 获取当前用户信息
     */
//    @GetMapping("/info/{phoneNumber}")
//    public R<SysUserAccount> getUserAccountInfoByPhone(@PathVariable("phoneNumber") String phoneNumber) {
//        SysUserAccount sysUserAccount = sysUserAccountService.selectSysUserAccountByPhone(phoneNumber);
//        return R.ok(sysUserAccount);
//    }

}
